#######################utils/MyBaseView.py#######################################
from syl_pro.settings import QINIU_ID, QINIU_SECRET
from qiniu import Auth

def qiniu_token():
    # 需要填写你的 Access Key 和 Secret Key
    access_key = QINIU_ID
    secret_key = QINIU_SECRET
    # 构建鉴权对象
    q = Auth(access_key, secret_key)
    # 要上传的空间
    bucket_name = 'syl-feifei'
    # 生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, expires=3600)
    return token

################################ oauthapp/views.py  #############################
from rest_framework.views import APIView
from rest_framework.response import Response
from utils.MyBaseView import qiniu_token

class QiNiuToken(APIView):

    # permission_classes = [IsAuthenticated]

    def get(self, request):
        token = qiniu_token()
        print(token)
        res_data = {
            "code": 200,
            "msg": "获取token成功",
            "data": {
                "uptoken": token
            }
        }
        return Response(res_data)
#########################  oauthapp/urls.py #########################
from django.urls import path, include
from oauthapp import views

router = DefaultRouter()

urlpatterns = [
    path('qntoken/', views.QiNiuToken.as_view()), # 获取七牛云token
]
